#!/bin/bash
ENVIRON=`env | sed "s/^/\"/" | sed "s/$/\"/" | sed "s/=/\" |-> \"/" | tr "\n" " "`
DIR=$(dirname $0)
PYTHON_BINARY=`which python3.3`
PYTHON_PREFIX=${PYTHON_BINARY%/bin/python3.3}
PYTHON_LIB=$PYTHON_PREFIX/lib/python3.3
#IMPORTLIB=$PYTHON_LIB/importlib/_bootstrap.py
IMPORTLIB=$DIR/fake-importlib.py
SIXTYFOUR_BIT='"sys.hash_info.width" |-> 64 "sys.hash_info.modulus" |-> 2305843009213693951 "sys.hash_info.inf" |-> 314159 "sys.hash_info.nan" |-> 0 "sys.hash_info.imag" |-> 1000003 "tuple.__hash__:begin" |-> 3430008 "tuple.__hash__:addend" |-> 82520 "tuple.__hash__:end" |-> 97531'
DEBUG='"PYTHONOPTIMIZE" |-> 0'
VERBOSE='"PYTHONVERBOSE" |-> false'
DEFAULT_BUFFER_SIZE='"_io.DEFAULT_BUFFER_SIZE" |-> 8192'
HASHINFO=$SIXTYFOUR_BIT
GC_THRESHOLD=100
PROGRAM_DIR="\"\""
# if we pass a program to kpython, sys.path[0] should be the absolute path to the directory the program is in
SYS_ARGV=""
PAST=
for var in "$@"; do
  if [ "$PAST" -a "$SYS_ARGV" = "" ]; then
    PROGRAM_DIR=`dirname $var | xargs readlink -f`
    PROGRAM_DIR=`printf "\"%q\"" $PROGRAM_DIR`
  fi
  if [ "$PAST" ]; then
    SYS_ARGV="$SYS_ARGV ListItem(\"$var\")"
  fi
  if [ "$var" = "--" -a "$SYS_ARGV" = "" ]; then
    PAST=1
  fi
done
if [ "$SYS_ARGV" = "" ]; then
  SYS_ARGV=".List"
fi
SYS_PATH="ListItem($PROGRAM_DIR) ListItem(\"$PYTHON_LIB\")"
HASHSEED='"PYTHONHASHSEED" |-> 1'
ERRNO=`python3.3 $DIR/errno.py`
OS='"sys.platform" |-> "linux" "posix.O_WRONLY" |-> 1 "posix.O_CREAT" |-> 64 "posix.O_EXCL" |-> 128'
ulimit -s unlimited
krun --compiled-def "$DIR/python-kompiled" --parser "$DIR/parse.py" -cGCTHRESHOLD="$GC_THRESHOLD" -cSYSPATH="$SYS_PATH" -cSYSARGV="$SYS_ARGV" -cCONSTANTS="$SIXTYFOUR_BIT $DEBUG $VERBOSE $HASHINFO $HASHSEED $OS $ERRNO $DEFAULT_BUFFER_SIZE" -cENVIRON="$ENVIRON" -cSEMANTICSDIR=" \"$DIR/\" " --cfg-parser="$DIR/parse.py" -cIMPORTLIB="$IMPORTLIB" --cfg-parser="kast -groundParser -e" "$@"
